package com.cuteboy.admin.service;

import com.cuteboy.dao.SysRoleMapper;
import com.cuteboy.entity.SysPermission;
import com.cuteboy.entity.SysRole;
import com.cuteboy.entity.SysRolePermission;
import com.cuteboy.util.ValidateUtils;
import com.cuteboy.vo.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysRoleService {

    @Autowired
    @Qualifier("sysRoleMapper")
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysRolePermissionService sysRolePermissionService;
    @Resource
    private SysPermissionService sysPermissionService;

    /**
     * 分页查找
     * @param name
     * @param page
     * @param limit
     * @return
     */
    public List<SysRole> page(String name, Integer page, Integer limit){

        Example sysRoleExample = new Example(SysRole.class);
        if (ValidateUtils.isNotEmpty(name)){
            sysRoleExample.or().andEqualTo("name",name);
        }

        PageHelper.startPage(page, limit);
        List<SysRole> sysRoles = sysRoleMapper.selectByExample(sysRoleExample);
        for (SysRole sysRole : sysRoles){
            packageVO(sysRole);
        }
//        Page<SysRole> pages = new Page<>(page, limit, totalNum);
//        pages.setItems(sysRoles);
        return sysRoles;
    }

    private void packageVO(SysRole sysRole) {
        List<SysRolePermission> sysRolePermissions = sysRolePermissionService.listRolePermiByRoleId(sysRole.getId());
        List<String> sysRolePermiNames = new ArrayList<>();
        for(SysRolePermission sysRolePermission : sysRolePermissions){
            SysPermission sysPermission = sysPermissionService.getEntity(sysRolePermission.getPermissionCode());
            if (ValidateUtils.isNotNull(sysPermission)){
                sysRolePermiNames.add(sysPermission.getName());
            }
        }
        sysRole.setRolePermission(sysRolePermiNames.toString());
    }

    /**
     * 获取所有角色
     * @return
     */
    public List<SysRole> selectAll(){
        return sysRoleMapper.selectAll();
    }

    /**
     * 获取实体
     * @return
     */
    public SysRole getEntity(Integer id){
        return sysRoleMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param entity
     * @return
     */
    public SysRole save(SysRole entity){
        sysRoleMapper.insert(entity);
        return entity;
    }

    /**
     * 删除
     * @return
     */
    public void delete(Integer id){
        // 删除角色绑定的权限
        sysRolePermissionService.deleteByRoleId(id);
        // 删除角色
        sysRoleMapper.deleteByPrimaryKey(id);
    }

    /**
     * 编辑
     * @return
     */
    public SysRole update(SysRole entity){
        sysRoleMapper.updateByPrimaryKey(entity);
        return entity;
    }




    public List<SysRole> selectRolesByUsername(String username){
        return sysRoleMapper.selectRolesByUsername(username);
    }
    public SysRole selectByPrimaryKey(Integer sysRoleId){
        return sysRoleMapper.selectByPrimaryKey(sysRoleId);
    }

    public Integer countAll(){
        return sysRoleMapper.countAll();
    }


}
